﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

using PercolationTree.Model;
using PercolationTree.Util;

namespace PercolationTree.Experiments
{
    class ClastersExperiment : IExperiment
    {
        private int m_MaxChildrenCount;
        private int m_MaxLayersCount;
        public ClastersExperiment(int maxChildrenCount, int maxLayersCount)
        {
            m_MaxChildrenCount = maxChildrenCount;
            m_MaxLayersCount = maxLayersCount;
        }

        public IList<Point> process(FactoryFunction factoryFunc)
        {
            const int kExpCount = 100;
            IList<Point> result = new List<Point>();
            Tree tree = factoryFunc(m_MaxChildrenCount, m_MaxLayersCount);
            while (Statistics.getNodesNumber(tree) < 200)
                tree = factoryFunc(m_MaxChildrenCount, m_MaxLayersCount);

            for (int p = 0; p <= 100; p += 9)
            {
                int value = 0;
                for (int k = 0; k < kExpCount; k++)
                {
                    int count = 0;
                    tree.Percolate(p / 100.0f);
                    for (int i = 0; i < tree.Layers.Count; i++)
                    {
                        Layer layer = tree.Layers[i];
                        foreach (Node node in layer.Nodes)
                        {
                            if (node.Type == NodeType.Invalid && node.Supernode.Type != NodeType.Invalid)
                                count++;
                        }
                    }
                    value += count;
                }
                value /= kExpCount;
                result.Add(new Point(p, value));
            }

            return result;
        }
    }
}
